﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.group
{
    public partial class operatoredit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Request["userid"]))
            {
                throw new Base.CustomException("缺少参数userid");
            }

            Hashtable edituser = PhoneBook.Users.Users.get(Request["userid"]);
            if (edituser == null)
            {
                throw new Base.CustomException("参数userid不正确");
            }

            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
            if (!user["groupid"].Equals(edituser["groupid"]) || edituser["usertype"].ToString() != "2" && edituser["usertype"].ToString() != "3")
            {
                throw new Base.CustomException("无权限修改此用户");
            }

            if (!IsPostBack)
            {
                //分公司
                if (user["isbooper"] != DBNull.Value || Convert.ToBoolean(user["isbooper"]))
                {
                    Hashtable bo = PhoneBook.Group.BranchOffice.get(Commons.StrUtil.FormatValue(user["bo"]));

                    ddlBO.DataSource = bo;
                    ddlBO.DataTextField = "boname";
                    ddlBO.DataValueField = "id";
                    ddlBO.DataBind();
                }
                else
                {
                    DataSet ds = PhoneBook.Group.BranchOffice.list(Commons.StrUtil.FormatValue(user["groupid"]));

                    Commons.ControlUtil.BindDDL(ddlBO, ds, "boname", "id", "非分公司", "");
                }


                txb_username.Text = Commons.StrUtil.FormatValue(edituser["username"]);
                txb_nickname.Text = Commons.StrUtil.FormatValue(edituser["nickname"]);

                DataSet roles = PhoneBook.Manage.SysRole.getGroupRole(user["groupid"].ToString());
                Commons.ControlUtil.BindDDL(ddl_roleid, roles, "rolename", "roleid");

                Hashtable sysuserrole = PhoneBook.Manage.SysUserRole.getuserrole(Request["userid"]);
                if (sysuserrole != null &&
                ddl_roleid.Items.FindByValue(Commons.StrUtil.FormatValue(sysuserrole["roleid"])) != null)
                    ddl_roleid.SelectedValue = Commons.StrUtil.FormatValue(sysuserrole["roleid"]);

                chkIsBOOper.Checked = (edituser["IsBOOper"] == DBNull.Value) ? false : Convert.ToBoolean(edituser["IsBOOper"]);
                ddlBO.Enabled = (edituser["IsBOOper"] == DBNull.Value) ? false : Convert.ToBoolean(edituser["IsBOOper"]);
                ddlBO.SelectedValue = Commons.StrUtil.FormatValue(edituser["bo"]);
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            if (!txb_userpwd.Text.Equals(txb_userpwd2.Text))
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.attention, "两次的密码不一致");
                return;            }

            Hashtable newuser = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Request["userid"]));
            addLog(newuser);
            newuser["username"] = txb_username.Text;
            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();

            if (txb_userpwd.Text != string.Empty)
                newuser["userpwd"] = txb_userpwd.Text;//encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text));

            //newuser["userpwd"] = encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text));
            newuser["nickname"] = txb_nickname.Text;

            newuser["isbooper"] = chkIsBOOper.Checked;

            if (chkIsBOOper.Checked)
            {
                if (string.IsNullOrEmpty(ddlBO.SelectedValue))
                {
                    Helper.ShowMessage(u_message, controls.UCMessageType.attention, "请选择分公司");
                    return;
                }
                newuser["bo"] = ddlBO.SelectedValue;
            }
            else
            {
                newuser["bo"] = null;
            }

            PhoneBook.Users.Users.edit(newuser);

            if (!string.IsNullOrWhiteSpace(ddl_roleid.SelectedValue))
            {
                string userid = PhoneBook.Users.Users.getByName(newuser["username"].ToString())["userid"].ToString();
                string roleid = ddl_roleid.SelectedValue;
                PhoneBook.Manage.SysUserRole.add(userid, roleid);
            }
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }

        //添加日志
        private void addLog(Hashtable user)
        {
            string content = string.Empty;

            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();

            if (user["username"].ToString() != txb_username.Text)
                content += string.Format("用户名:{0}->{1};", user["username"], txb_username.Text);

            if (txb_userpwd.Text != string.Empty)
            {
                if (user["userpwd"].ToString() != encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text)))
                    //content += string.Format("密码:{0}->{1};", user["userpwd"], encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text)));
                    content += string.Format("密码变更;");
            }

            if ((user["isbooper"] == null || !Convert.ToBoolean(user["isbooper"])) && chkIsBOOper.Checked)
            {
                content += string.Format("管理员身份更改为{0}分公司管理员", ddlBO.SelectedItem.Text);
            }
            else if (Convert.ToBoolean(user["isbooper"]) && !chkIsBOOper.Checked)
            {
                content += string.Format("管理员身份更改为分集团管理员");
            }

            if (user["nickname"].ToString() != txb_nickname.Text)
                content += string.Format("昵称:{0}->{1};", user["nickname"], txb_nickname.Text);

            if (!string.IsNullOrEmpty(content))
            {
                content = string.Format("修改了管理员信息({0})。{1}", user["username"], content);

                AddLog(content);
            }
        }
    }
}